from time import sleep
from study import study
import re

page_login = "https://www.hnpxw.org/index?login=true"
jy = study()
jy.driver.get(page_login)
sleep(5)
jy.inputText('//*[@id="app-container"]/div[3]/div/form/div[1]/div/div[1]/input', 'qvbucg')
jy.inputText('//*[@id="app-container"]/div[3]/div/form/div[2]/div/div/input', 'oditszXC102298')
jy.clickElement('//*[@id="app-container"]/div[3]/div/form/div[5]/button')
sleep(5)
jy.clickElement('//*[@id="pane-first"]/div/div[1]/div/div/div/div[2]/div[2]')
sleep(5)

while True:
    sleep(10)
    el_refresh = jy.getElement('//span[contains(text(), "更新学习进度")]')
    el_refresh = jy.getElementByText('//span', "更新学习进度")
    
    if el_refresh != None:
        jy.clickFindedElement(el_refresh)
        print("更新学习进度")
        sleep(10)
    
    scores = jy.getElements('//*[@id="app-container"]/div[2]/div/div[2]/div/div[3]/table/tbody/tr/td[4]/div/p')
    buttons = jy.getElements('//*[@id="app-container"]/div[2]/div/div[2]/div/div[3]/table/tbody/tr/td[5]/div/button/span')
    notfinish = []
    
    for i in range(len(scores)):
        if scores[i].text != '100':
            notfinish.append(buttons[i])

    print("一共有%d个未学课程" %(len(notfinish)))
    
    notfinish[0].click()
    sleep(5)

    while True:
        el_play = jy.getElement('//div[@class="mejs__overlay-button"]')
        el_tips = jy.getElements('//div[text()="跳过所有提示"]')
        el_know = jy.getElement('//button[text()="知道了"]')
        el_studyover = jy.getElement('//span[text()="已看完"]')
        el_nextpage = jy.getElement('//div[@class="btn-tip"]/span[text()="下一页"]')
        el_staythispage = jy.getElement('//span[text()="留在本页"]')
        el_close = jy.getElement('//span[text()="关闭"]')
        el_return = jy.getElement('//span[text()="返回课程章节"]')
        el_pause = jy.getElement('//div[contains(text(),"计时学习已暂停")')
        el_continue = jy.getElement('//div[contains(text(),"继续学习")')
        el_test = jy.getElement('//div[text()="练习题"]')
        el_leave = jy.getElement('//button[contains(.,"确定离开")]')
        
        
        if el_play != None:
            jy.clickFindedElement(el_play)
        
        if el_tips != None:
            jy.clickFindedElements(el_tips)

        if el_know != None:
            jy.clickFindedElement(el_know)
            
        if el_studyover != None:
            if el_nextpage != None:
                jy.clickFindedElement(el_nextpage)
        
        if el_pause != None:
            if el_continue != None:
                jy.clickFindedElement(el_continue)
    
        if el_test != None:
            if el_nextpage != None:
                jy.clickFindedElement(el_nextpage)
        
        if el_leave != None:
            jy.clickFindedElement(el_leave)
            
        if el_staythispage != None:
            if el_close != None:
                jy.clickFindedElement(el_close)
                if el_return != None:
                    jy.clickFindedElement(el_return)
                break